'use strict';
/**
 * 配置校验是否登录
 * @type
 */
var _me = angular.noop;
angular.module('app').constant('resolver', {
    checkLogin: function () {
        return _me();
    }
});
angular.module('app').run(function ($http, $q) {
    _me = function () {
        if (angular.isUndefined(window.appCheckLoginedCount)) {
            window.appCheckLoginedCount = 0;
        }
        window.appCheckLoginedCount++;
        if (window.appCheckLoginedCount == 1) {
            var promise_ = $http.get(ISS.getPath('checkLogined'));
            promise_.then(function (resp) {
                window.appCheckLoginedCount--;
                return resp;
            }, function (resp) {
                window.appCheckLoginedCount--;
                return resp;
            });
            return promise_;

        } else {
            var deferred = $q.defer();
            setTimeout(function () {
                deferred.reject('Hello!');
            }, 50);
            deferred.promise.then(function (resp) {
                window.appCheckLoginedCount--;
                return resp;
            }, function (resp) {
                window.appCheckLoginedCount--;
                return resp;
            });
            return deferred.promise;
        }

    }
});
/**
 * Config for the router
 */
angular.module('app')
    .run(
        ['$rootScope', '$state', '$stateParams',
            function ($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider', 'resolver',
            function ($stateProvider, $urlRouterProvider, resolver) {
                $urlRouterProvider.otherwise('/login');
                $stateProvider
                    .state('app', {
                        abstract: true,
                        url: '/app',
                        templateUrl: ISS.getResPath('app/app.html'),
                        resolve: {
                            me: resolver.checkLogin,
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('js/controllers/setting/HeaderCtrl.js'),
                                        ISS.getResPath('app/modules/basic/infocenter/modals/InfoDetailCtrl.js'),
                                        ISS.getResPath('app/modules/basic/infocenter/InfocenterService.js'),
                                        ISS.getResPath('app/modules/basic/infocenter/InfocenterCtrl.js')
                                    ]).then(function () {
                                    });
                                }]
                        }
                    })
                    .state('login', {
                        url: '/login',
                        templateUrl: ISS.getResPath('app/login.html'),
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load([
                                        ISS.getResPath('js/controllers/LoginCtrl.js')
                                    ]).then(function () {
                                    });
                                }]
                        }
                    })
                    .state('app.block', {
                        url: '/block',
                        templateUrl: ISS.getResPath('tpl/blocks/block.html'),
                        controller: 'BlockCtrl',
                        resolve: {
                            me: resolver.checkLogin,
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('js/controllers/BlockCtrl.js')
                                    ]).then(function () {
                                    });
                                }]
                        }
                    })

                    .state('setting', {
                        abstract: true,
                        url: '/setting',
                        templateUrl: ISS.getResPath('app/setting.html'),
                        resolve: {
                            me: resolver.checkLogin,
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('js/controllers/setting/HeaderCtrl.js'),
                                        ISS.getResPath('app/modules/basic/infocenter/modals/InfoDetailCtrl.js'),
                                        ISS.getResPath('app/modules/basic/infocenter/InfocenterService.js'),
                                        ISS.getResPath('app/modules/basic/infocenter/InfocenterCtrl.js')
                                    ]).then(function () {
                                    });
                                }]
                        }
                    })
                    .state('setting.block', {
                        url: '/block',
                        templateUrl: ISS.getResPath('tpl/blocks/block.html'),
                        controller: 'SettingBlockCtrl',
                        resolve: {
                            me: resolver.checkLogin,
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('js/controllers/setting/SettingBlockCtrl.js')
                                    ]).then(function () {
                                    });
                                }]
                        }
                    })
                    .state('setting.log', {
                        url: '/log',
                        templateUrl: ISS.getResPath('app/modules/log/log-list.html'),
                        controller: 'LogCtrl',
                        resolve: {
                            me: resolver.checkLogin,
                            deps: ['$ocLazyLoad', 'LoginService',
                                function ($ocLazyLoad, LoginService) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('app/modules/log/LogCtrl.js')
                                    ]).then(function () {
                                        return LoginService.hasPermission('M_LOG');
                                    });
                                }]
                        }
                    })

                    .state('setting.user', {
                        url: '/user',
                        template: '<div ui-view></div>',
                        resolve: {
                            deps: ['$ocLazyLoad', 'LoginService',
                                function ($ocLazyLoad, LoginService) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('app/modules/user/UserService.js'),
                                        ISS.getResPath('app/modules/user/UserCtrl.js')
                                    ]).then(function () {
                                        return LoginService.hasPermission('M_USER');
                                    });
                                }]
                        }
                    })
                    .state('setting.user.list', {
                        url: '/list',
                        templateUrl: ISS.getResPath('app/modules/user/user-list.html')
                    })
                    .state('setting.user.add', {
                        url: '/add',
                        templateUrl: ISS.getResPath('app/modules/user/user-add-edit.html')
                    })
                    .state('setting.user.update', {
                        url: '/update/{id}',
                        templateUrl: ISS.getResPath('app/modules/user/user-add-edit.html')
                    })
                    .state('setting.role', {
                        url: '/role',
                        template: '<div ui-view ></div>',
                        resolve: {
                            deps: ['$ocLazyLoad', 'LoginService',
                                function ($ocLazyLoad, LoginService) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('app/modules/role/RoleService.js'),
                                        ISS.getResPath('app/modules/role/RoleCtrl.js')
                                    ]).then(function () {
                                        return LoginService.hasPermission('M_ROLE');
                                    });
                                }]
                        }
                    })
                    .state('setting.role.list', {
                        url: '/list',
                        templateUrl: ISS.getResPath('app/modules/role/role-list.html')
                    })
                    .state('setting.role.add', {
                        url: '/add',
                        templateUrl: ISS.getResPath('app/modules/role/add-edit-role.html')
                    })
                    .state('setting.role.update', {
                        url: '/update/{id}',
                        templateUrl: ISS.getResPath('app/modules/role/add-edit-role.html')
                    })
                    .state('setting.infoset', {
                        url: '/infoset',
                        template: '<div ui-view ></div>',
                        resolve: {
                            deps: ['$ocLazyLoad', 'LoginService',
                                function ($ocLazyLoad, LoginService) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('app/modules/basic/infoset/InfoSetCtrl.js'),
                                        ISS.getResPath('app/modules/basic/infoset/InfoSetService.js')
                                    ]).then(function () {
                                        //return LoginService.hasPermission('M_INFOSET');
                                    });
                                }]
                        }
                    })
                    .state('setting.infoset.list', {
                        url: '/list',
                        templateUrl: ISS.getResPath('app/modules/basic/infoset/infoset-list.html')
                    })
                    .state('setting.infoset.add', {
                        url: '/add',
                        templateUrl: ISS.getResPath('app/modules/basic/infoset/add-edit-infoset.html')
                    })
                    .state('setting.infoset.update', {
                        url: '/update/{id}',
                        templateUrl: ISS.getResPath('app/modules/basic/infoset/add-edit-infoset.html')
                    })
                    .state('setting.personal', {
                        url: '/personal',
                        templateUrl: ISS.getResPath('app/modules/personal/account.html'),
                        resolve: {
                            me: resolver.checkLogin,
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        'ngImgCrop',
                                        ISS.getResPath('app/modules/personal/PersonalService.js'),
                                        ISS.getResPath('app/modules/personal/PersonalCtrl.js')
                                    ]).then(function () {
                                        return $ocLazyLoad.load('app/modules/personal/imgcrop.js');
                                    });
                                }]
                        }
                    })
                    .state('setting.sysconfig', {
                        url: '/sysconfig',
                        templateUrl: ISS.getResPath('tpl/blocks/setting/sysconfig/sysconfig-list.html'),
                        resolve: {
                            deps: ['$ocLazyLoad', 'LoginService',
                                function ($ocLazyLoad, LoginService) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('js/services/setting/SysConfigService.js'),
                                        ISS.getResPath('js/controllers/setting/SysConfigCtrl.js')
                                    ]).then(function () {
                                        return LoginService.hasPermission('M_SYSTEMCONFIG');
                                    });
                                }]
                        }
                    })
                    /**
                     * 区域管理-路由
                     */
                    .state('setting.area', {
                        url: '/area',
                        template: '<div ui-view ></div>',
                        resolve: {
                            deps: ['uiLoad', 'LoginService',
                                function (uiLoad, LoginService) {

                                    var loads = ISS.getMapPath();

                                    loads.push(ISS.getResPath('app/modules/area/AreaService.js'));
                                    loads.push(ISS.getResPath('app/modules/area/AreaCtrl.js'));
                                    return uiLoad.load(loads).then(function () {
                                        return LoginService.hasPermission('M_ARCH').then(function () {
                                            return ISS.loadMap();
                                        });
                                    });
                                }]
                        }
                    })
                    .state('setting.area.list', {
                        url: '/list',
                        templateUrl: ISS.getPath('app/modules/area/area-list.html')
                    })
                    .state('setting.area.add', {
                        url: '/add',
                        templateUrl: ISS.getPath('app/modules/area/add-edit.html')
                    })
                    .state('setting.area.update', {
                        url: '/update/{id}',
                        templateUrl: ISS.getPath('app/modules/area/add-edit.html')
                    })
                    .state('setting.device', {
                        url: '/device',
                        template: '<div ui-view ></div>',
                        resolve: {
                            deps: ['uiLoad', 'LoginService',
                                function (uiLoad, LoginService) {

                                    var loads = ISS.getMapPath();

                                    loads.push(ISS.getResPath('app/modules/device/DeviceService.js'));
                                    loads.push(ISS.getResPath('app/modules/device/DeviceCtrl.js'));
                                    return uiLoad.load(loads).then(function () {
                                        return LoginService.hasPermission('M_ARCH').then(function () {
                                            return ISS.loadMap();
                                        });
                                    });
                                }]
                            /* deps: ['$ocLazyLoad',
                             function ($ocLazyLoad) {
                             return $ocLazyLoad.load([
                             ISS.getResPath('app/modules/device/DeviceService.js'),
                             ISS.getResPath('app/modules/device/DeviceCtrl.js')
                             ]).then(function () {
                             });
                             }]*/
                        }
                    })
                    .state('setting.device.list', {
                        url: '/list',
                        templateUrl: ISS.getResPath('app/modules/device/device-list.html')
                    })
                    .state('setting.device.add', {
                        url: '/add',
                        templateUrl: ISS.getResPath('app/modules/device/device-add-edit.html')
                    })
                    .state('setting.device.update', {
                        url: '/update/{id}',
                        templateUrl: ISS.getResPath('app/modules/device/device-add-edit.html')
                    })
                    .state('setting.device.set', {
                        url: '/set',
                        templateUrl: ISS.getResPath('app/modules/device/device-set.html')
                    })
                    // 数据字典
                    .state('setting.dict', {
                        url: '/dict',
                        template: '<div ui-view ></div>',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        ISS.getResPath('app/modules/dict/DictService.js'),
                                        ISS.getResPath('app/modules/dict/I18nService.js'),
                                        ISS.getResPath('app/modules/dict/dict-list.js')
                                    ]).then(function () {
                                    });
                                }]
                        }
                    })
                    .state('setting.dict.list', {
                        url: '/list',
                        controller: 'DictListCtrl',
                        controllerAs: 'vm',
                        templateUrl: ISS.getResPath('app/modules/dict/dict-list.html')
                    })
                    .state('setting.dict.add', {
                        url: '/add',
                        templateUrl: ISS.getResPath('app/modules/dict/add-edit-dict.html')
                    })
                    .state('setting.dict.update', {
                        url: '/update/{id}',
                        templateUrl: ISS.getResPath('app/modules/dict/add-edit-dict.html')
                    });
            }
        ]
    );
